package com.diandi.activity.controller;

import com.alibaba.fastjson.JSON;
import com.diandi.activity.DateFormatUtils;
import com.diandi.activity.bean.LotteryTicket;
import com.diandi.activity.service.LotteryTicketService;
import com.diandi.ddsb.bean.Result;
import com.diandi.ddsb.bean.Staff;
import com.diandi.ddsb.bean.SystemConstant;
import com.diandi.ddsb.vo.Constant;
import com.diandi.ddsb.vo.LotteryTicketInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by a on 2018/3/27.
 */
@RestController
@RequestMapping("/lotteryTicket")
public class LotteryTicketController {
    private static Logger logger = org.slf4j.LoggerFactory.getLogger(LotteryTicketController.class);
    @Autowired
    private LotteryTicketService lotteryTicketService;
    /**
     * 访问奖券管理首页
     *
     * @return 奖券管理列表页
     */
    @RequestMapping("/lotteryTicketList")
    public ModelAndView goLotteryTicketList() {
        logger.info("访问奖券管理首页");
        return new ModelAndView("activity/lotteryTicketList");
    }
    /**
     * 返回奖券列表数据
     *
     * @return
     */
    @RequestMapping(value = "/queryLotteryTickets")
    public String queryLotteryTickets(@RequestParam(required = false) Long id,
                                      @RequestParam(required = false) String title, @RequestParam(required = false) String startTime,
                                      @RequestParam(required = false) String endTime, @RequestParam(required = false) String type,
                                      @RequestParam(required = false) String status, @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "20") Integer rows) {
        int from = (page - 1) * rows;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("id", id);
        paramMap.put("title", title);
        paramMap.put("type", type);
        paramMap.put("status", status);
        paramMap.put("from", from);
        paramMap.put("rows", rows);

        if (StringUtils.isNotBlank(startTime)) {
            paramMap.put("startTime", DateFormatUtils.reverseDate(startTime));
        }
        if (StringUtils.isNotBlank(endTime)) {
            paramMap.put("endTime", DateFormatUtils.reverseDate(endTime));
        }

        List<LotteryTicket> LotteryTickets = lotteryTicketService.queryLotteryTickets(paramMap);

        Integer activityInfoCount = lotteryTicketService.queryLotteryTicketCount(paramMap);
        Map dataGridJsonData = new HashMap();
        dataGridJsonData.put("total", activityInfoCount);
        dataGridJsonData.put("rows", LotteryTickets);
        return JSON.toJSONString(dataGridJsonData);
    }


    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/insertLotteryTicket")
    public String insertLotteryTicket(LotteryTicketInfoVo lotteryTicket) {
        Result result = new Result();
        try {
            result = lotteryTicketService.insertLotteryTicket(lotteryTicket);
        } catch (RuntimeException e) {
            e.printStackTrace();
            result.setCode(Constant.FAIL);
            result.setMsg("新增奖券失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Constant.FAIL);
            result.setMsg("新增奖券失败" + e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    /**
     * 查看奖券详细信息
     *
     */
    @RequestMapping(value = "/lotteryTicketDetail")
    public ModelAndView lotteryTicketDetail(@RequestParam Long id, @RequestParam String editFlag) throws Exception {
        LotteryTicketInfoVo lotteryTicketInfoVo = lotteryTicketService.getLotteryTicketById(id);
        lotteryTicketInfoVo.setEditFlag(editFlag);
        ModelAndView mv = new ModelAndView();
        mv.addObject("lotteryTicketInfoVo", lotteryTicketInfoVo);
        mv.setViewName("activity/lotteryTicketIndex");
        return mv;
    }
    /**
     * 更新奖券信息
     *
     */
    @RequestMapping(value = "/updateLotteryTicket", method = RequestMethod.POST)
    public String updateLotteryTicket(@RequestParam(required = false)  String id ,@RequestParam(required = false) String  status) {
        Result result = new Result();
        LotteryTicketInfoVo lotteryTicket = new LotteryTicketInfoVo();
        lotteryTicket.setId(Long.parseLong(id));
        lotteryTicket.setStatus(Integer.parseInt(status));
        Staff staff = new Staff();
        staff.setId(-1l);
        if (staff == null) {
            result.setCode(Constant.FAIL);
            result.setMsg("请重新登录系统!");
            return JSON.toJSONString(result);
        }
        int rtn = lotteryTicketService.updateLotteryTicket(lotteryTicket);

        if (rtn == SystemConstant.FINAL_INT_ZERO) {
            result.setCode(Constant.FAIL);
            result.setMsg("奖券信息已不是最新版本！");
        } else if (rtn < SystemConstant.FINAL_INT_ZERO) {
            result.setCode(Constant.FAIL);
            result.setMsg("奖券信息更新失败！");
        } else {
            result.setCode(Constant.SUCCESS);
            result.setMsg("奖券信息更新成功！");
        }
        return JSON.toJSONString(result);
    }
    /**
     * 激活用户
     *
     */
    @RequestMapping(value = "/ticketCombox")
    public Object ticketCombox() {
        return lotteryTicketService.queryTicketCombox();
    }

}
